package client.connection;

import helper.request.Request;
import helper.response.Response;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientConnector {
	private String serveraddress;
	private static final int serverport = 23456;
	private Socket sockettoserver;

	public ClientConnector(String address) {
		this.serveraddress = address;
	}

	private void opensocket() throws IOException {
		sockettoserver = new Socket(serveraddress, serverport);
	}

	private void closesocket() throws IOException {
		sockettoserver.close();
	}

	public Response process(Request req) throws Exception {
		// TODO unfinished
		try {
			opensocket();
			ObjectOutputStream oos=new ObjectOutputStream(sockettoserver.getOutputStream());
			oos.writeObject(req);
			ObjectInputStream ois=new ObjectInputStream(sockettoserver.getInputStream());
			Response res=(Response) ois.readObject();
			closesocket();
			return res;
		} catch (IOException ex) {
			throw ex;	
		}
	}
}
